package com.lincoln.server.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@SpringBootApplication
@RestController
@EnableDiscoveryClient
@EnableFeignClients
@CrossOrigin
public class UserServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserServerApplication.class, args);
    }

    @Value("${server.port}")
    private int port;

    public static Map<String, String> loginUser = new HashMap<>();

    public static Map<String, String> userMap = new HashMap<>();

    public static Map<String, String> codeMap = new HashMap<>();

    @Autowired
    FeignService feignService;

    @GetMapping("/sendCode")
    public String sendCode(String username) throws UnknownHostException {
        try {
            if (userMap.get(username) != null) {
                return "[本次查询由服务器" + InetAddress.getLocalHost().getHostAddress() + ":" + port + "处理->" + "用户已存在!]";
            } else {
                String code = "123456";
                codeMap.put(username, code);
                String res = feignService.sendSms("[本次短信验证码由服务器" + InetAddress.getLocalHost().getHostAddress() + ":" + port + "生成,验证码是{" + code + "}]");
                return res;
            }
        } catch (Exception e) {
            return "[本次查询由服务器" + InetAddress.getLocalHost().getHostAddress() + ":" + port + "处理->" + "系统错误]";
        }
    }

    @GetMapping("/register")
    public String register(String username, String password, String code) throws UnknownHostException {
        try {
            if (username == null) {
                throw new Exception();
            }
            if (userMap.get(username) == null) {
                if (code.equals(codeMap.get(username))) {
                    userMap.put(username, password);
                    return "[本次查询由服务器" + InetAddress.getLocalHost().getHostAddress() + ":" + port + "处理->" + "注册成功]";
                } else {
                    return "[本次查询由服务器" + InetAddress.getLocalHost().getHostAddress() + ":" + port + "处理->" + "注册失败,短信验证码错误!]";
                }
            } else {
                return "[本次查询由服务器" + InetAddress.getLocalHost().getHostAddress() + ":" + port + "处理->" + "用户已存在!]";
            }
        } catch (Exception e) {
            return "[本次查询由服务器" + InetAddress.getLocalHost().getHostAddress() + ":" + port + "处理->" + "系统错误]";
        }
    }

    @GetMapping("/checkLogin")
    public String checkLogin(String username, String password) throws UnknownHostException {
        try {
            if (password.equals(userMap.get(username))) {
                String uuid = UUID.randomUUID().toString();
                loginUser.put(uuid, username);
                return uuid;
            }
        } catch (Exception e) {
            return "false";
        }
        return "false";
    }

    @GetMapping("/getInfo")
    public String getInfo(String token) throws UnknownHostException {
        return "[本次查询由服务器" + InetAddress.getLocalHost().getHostAddress() + ":" + port + "处理,用户信息为:{用户名是" + loginUser.get(token) + "}]";
    }
}
